package edu.dublbo.commons.lang;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 5591475079337619026L;
    private int code;
    private String msg;
    private Object data;

    private static <T> Result<T> build(int code, String msg, T data) {
        return new Result<>(code, msg, data);
    }

    // -------------- success --------------- //
    public static <T> Result<T> success() {
        return build(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), null);
    }
    public static <T> Result<T> success(T data) {
        return build(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), data);
    }
    public static <T> Result<T> success(String msg, T data) {
        return build(ResultEnum.SUCCESS.getCode(), msg, data);
    }

    // -------------- failure --------------- //
    public static <T> Result<T> fail() {
        return build(ResultEnum.FAILURE.getCode(), ResultEnum.FAILURE.getMsg(), null);
    }

    public static <T> Result<T> fail(String msg) {
        return build(ResultEnum.FAILURE.getCode(), msg, null);
    }

    public static <T> Result<T> fail(String msg, T data) {
        return build(ResultEnum.FAILURE.getCode(), msg, data);
    }
}
